package cn.lj.service.impl;

import cn.lj.dao.BlogDao;
import cn.lj.dao.CommentDao;
import cn.lj.pojo.Comment;
import cn.lj.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author lj
 * @Description:
 * @date 2020-11-29 14:05
 * @QQ 851477108
 */

@Service
@Transactional
public class CommentServiceImpl implements CommentService {

    @Autowired
    private CommentDao commentDao;

    @Autowired
    private BlogDao blogDao;

    /**
     * 获得一个博客的所有评论
     * @param blogId
     * @return
     */
    @Override
    public List<Comment> getCommentByBlogId(Long blogId) {
        //根据创建时间倒序来排
        List<Comment> comments = commentDao.findByBlogIdAndParentCommentNull(blogId, Long.parseLong("-1"));//没有父节点的默认为-1
        return comments;
    }

    /**
     * 保存评论，接收回复的表单
     * @param comment
     * @return
     */
    @Override
    public int saveComment(Comment comment) {
        //获得父id
        Long parentCommentId = comment.getParentCommentId();
        //没有父级评论默认是-1
        if (parentCommentId != -1) {
            //有父级评论
            comment.setParentComment(commentDao.findByParentCommentId(comment.getParentCommentId()));
        }else {
            //没有父级评论
            comment.setParentCommentId((long) -1);
            comment.setParentComment(null);
        }
        comment.setCreateTime(new Date());
        return commentDao.saveComment(comment);
    }
}
